<?php

//*******************************************************
//* JAccelerator Component
//* http://fun.kubera.org
//* (C) Radoslaw Kubera
//* license http://www.gnu.org/copyleft/gpl.html GNU/GPL
//*******************************************************

defined('_VALID_MOS') or die();

$params = array();

$params['default_cache_time'] = $mosConfig_cachetime;
$path_config = $mosConfig_absolute_path.'/components/com_joomlacache/config.php';
if(file_exists($path_config))
	include $path_config;

$time = $params['default_cache_time'];
$type = 0;
$JoomlaCacheBaypass = JoomlaCacheBaypass($time, $type);
$cachetime = $time;
$joomlacache_show = 1;
$joomlacache_write = 0;

$url_joomlacache = $_SERVER['CACHE_REQUEST_URI'];
$url_joomlacache = $mosConfig_lang.$url_joomlacache;

$path_joomlacache_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/';
$path_joomlacache = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/files/';

if(!$JoomlaCacheBaypass) {
	if(!$my->id && $type == 1) {
		if(file_exists($path_joomlacache.$url_joomlacache)) {
			$time = filemtime($path_joomlacache.$url_joomlacache);
			if($time + $cachetime < mktime()) {
				if(file_exists($path_joomlacache.$url_joomlacache)) {
					$database->setQuery("delete from #__joomlacache where filename='$url_joomlacache' and component='1'");
					$database->query();

					@unlink($path_joomlacache.$url_joomlacache);
					$joomlacache_write = 1;
					ob_start();
				}

				$path_conf = $mosConfig_absolute_path.'/components/com_joomlacache/';
				if(file_exists($path_conf.'config.php')) {
					include ($path_conf.'config.php');
				}
				if(isset($params['flush_after_expired']))
					JoomlaCacheFlush($option);

			} else {
				$data = file($path_joomlacache.$url_joomlacache);
				$data = implode('', $data);
				echo $data;

				$joomlacache_show = 0;
				$count = 0;
				if(file_exists($mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/cache.log')) {
					include $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/cache.log';
				}
				$count++;
				$txt = "<?php\n";
				$txt .= "$"."count=".$count.";\n";
				$txt .= "$"."time=".$cachetime.";\n";
				$txt .= "?>";

				ob_start();
				$file = fopen($mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/cache.log', "w+");
				fwrite($file, $txt);
				fclose($file);
				ob_end_clean();
			}
		} else {
			$joomlacache_write = 1;
			ob_start();
		}
	}
}

if($joomlacache_show == 1) {
	$mainframe->_path->front = $_SERVER['cache_path'];
	if($path = $mainframe->getPath('front')) {
		$task = mosGetParam($_REQUEST, 'task', '');
		$ret = mosMenuCheck($Itemid, $option, $task, $gid);
		if($ret) {
			require_once ($path);
		} else {
			mosNotAuth();
		}
	} else {
		header("HTTP/1.0 404 Not Found");
		echo _NOT_EXIST;
	}
}

if($joomlacache_write == 1) {

	$data = ob_get_contents();
	ob_end_clean();
	echo $data;

	if(!isset($_SERVER['joomlacachelimit'])) {
		ob_start();
		$file = fopen($path_joomlacache.$url_joomlacache, "w+");
		fwrite($file, $data);
		fclose($file);

		global $database;
		$size = filesize($path_joomlacache.$url_joomlacache);
		$date = filemtime($path_joomlacache.$url_joomlacache);
		$database->setQuery("delete from #__joomlacache where filename='$url_joomlacache' and component='1'");
		$database->query();
		$date = date("Y-m-d h:i:s A", $date);
		$database->setQuery("insert into #__joomlacache set filename='$url_joomlacache', size='$size', date='$date', element='$option', component='1'");
		$database->query();
		ob_end_clean();
	}
}

if(is_array($params))
	if(isset($params['gentime']) && $params['gentime']) {
		echo '<div id="timestamp">[TIMESTAMP]</div>';
	}
$time_end = JoomlaCacheGetMicrotime();
$time = $time_end - $_SERVER['joomlacachestart'];

JoomlaCacheShowTimestamp($time);


?>
